package com.ehi.cuke.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.ehi.common.jersey.util.RestClientService;
import com.ehi.cuke.common.context.RestClientContext;

public class BaseStepDefs {

	@Value("${api.url}")
	protected String apiUrl;

	@Value("${api.ch.url}")
	protected String chapiUrl;

	@Value("${sep.start}")
	protected String sepStart;

	@Value("${sep.end}")
	protected String sepEnd;

	@Value("${test.db.url}")
	protected String dburl;

	@Value("${test.db.username}")
	protected String dbusername;

	@Value("${test.db.password}")
	protected String dbpw;

	protected String dbdriver = "oracle.jdbc.driver.OracleDriver";

	@Autowired
	protected RestClientContext restClientContext;

	@Autowired
	protected RestClientService restClientService;

	protected String EHI_VISITOR_ID = "ehiVisitorId";

	protected Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName(dbdriver);
			conn = DriverManager.getConnection(dburl, dbusername, dbpw);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return conn;
	}

	protected void closeConnection(Connection conn) {
		try {
			if (conn != null)
				if (!conn.isClosed()) {
					conn.close();
				}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	protected ResultSet queryDB(String sql) throws SQLException {
		Connection conn=getConnection();
		PreparedStatement ps=conn.prepareStatement(sql);
		ResultSet rs=ps.executeQuery();
		//closeConnection(conn);
		return rs;		
	}

}
